﻿using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AzureFileDepot.Helpers
{
    public class ServiceBusTopic
    {
        TopicClient sbTopic;
        SubscriptionClient sbSubscription;
        string accessSignature;
        string topicName; 

        public ServiceBusTopic(string accessSignature, string topicName)
        {
            this.accessSignature = accessSignature;
            this.topicName = topicName;

            sbTopic = TopicClient.CreateFromConnectionString(accessSignature, topicName);
        }

        public void SendMessage(string message)
        {
            sbTopic.Send(new BrokeredMessage(message));
        }

        public string GetMessage()
        {
            BrokeredMessage message = sbSubscription.Receive();
            // #DepotTip
            // if the recieve times out, we get a null message
            if (message != null)
            {
                message.Complete();

                return message.GetBody<string>();
            }
            return null; 
        }

        public void CreateSubscription(string subscriberName)
        {
            var namespaceManager = NamespaceManager.CreateFromConnectionString(this.accessSignature);

            if (!namespaceManager.SubscriptionExists(this.topicName, subscriberName))
            {
                SubscriptionDescription subDesc = new SubscriptionDescription(this.topicName, subscriberName)
                {
                    // #DepotTip
                    // we set this to make sure our subscriber gets cleaned up it the processes are shut down
                    // this creates a "volatile subsriber"
                    AutoDeleteOnIdle = new TimeSpan(0, 30, 0)
                };
                namespaceManager.CreateSubscription(subDesc);
            }

            sbSubscription = SubscriptionClient.CreateFromConnectionString(this.accessSignature, this.topicName, subscriberName);
        }

        /// <summary>
        /// This will be used on a future version
        /// </summary>
        public void StartMessagePump()
        {
            OnMessageOptions options = new OnMessageOptions();
            options.AutoComplete = true; // Indicates if the message-pump should call complete on messages after the callback has completed processing.
            options.MaxConcurrentCalls = 5; // Indicates the maximum number of concurrent calls to the callback the pump should initiate 

            Console.WriteLine("Starting message processing ...");
            // initial recieve of messages
            sbSubscription.OnMessage((receivedMessage) => 
            {
                // Process the message
                Console.WriteLine(string.Format("Processing recived Message: Id = {0}, Body = {1}", receivedMessage.MessageId, receivedMessage.GetBody<string>()));
            }, options);
        }
    }
}
